package com.qianfeng.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.Class;
import com.qianfeng.entity.Department;
import com.qianfeng.entity.Subject;
import com.qianfeng.mapper.ClassMapper;
import com.qianfeng.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassMapper classMapper;

    @Transactional
    @Override
    public void addClass(Class cla) {
        cla.setSubject(new Subject(cla.getSubjectId()));
        classMapper.addClass(cla);
    }

    @Transactional
    @Override
    public void deleteClass(Integer classId) {
        classMapper.deleteClass(classId);

    }

    @Transactional
    @Override
    public void updateClass(Class cla) {
        cla.setSubject(new Subject(cla.getSubjectId()));
        classMapper.updateClass(cla);
    }

    @Transactional(readOnly = true)
    @Override
    public Page getClassPage(Integer pageNo, Integer pageSize) {
        Page page = new Page();
        PageHelper.startPage(pageNo,pageSize);
        List<Class> classList = classMapper.getClassList();
        PageInfo<Class> info = new PageInfo<>(classList);
        page.setPageSize(info.getPageSize());
        page.setPageNo(info.getPageNum());
        page.setPageCount(info.getPages());
        page.setHasPre(info.isHasPreviousPage());
        page.setHasNext(info.isHasNextPage());
        page.setPage(info.getList());
        return page;
    }

    @Transactional(readOnly = true)
    @Override
    public Class getClassByClassId(Integer classId) {
        return classMapper.getClassByClassId(classId);
    }
}
